package JDBC_Test.Fruit.Service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.xml.transform.Source;

//import JDBC_Test.Book.Dao.RecordDao;
import JDBC_Test.Fruit.Dao.FruitDao;
import JDBC_Test.Fruit.Dao.FruitInDao;
import JDBC_Test.Fruit.Dao.FruitRecordDao;
import JDBC_Test.Fruit.Model.FruitInMedel;
import JDBC_Test.Fruit.Model.FruitModel;
import JDBC_Test.Fruit.Model.SaleRecordModel;

public class FruitService {
	FruitDao fruitDao = new FruitDao();
	FruitInDao inDao = new FruitInDao();
	FruitRecordDao fruitRecodDao = new FruitRecordDao();

	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		FruitService m = new FruitService();
		m.print();
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("请输入操作：");
			String s1 = scanner.nextLine();
			int operation = Integer.valueOf(s1);
			if (operation == 0) {
				System.out.println("感谢使用，再见");
				break;
			}
			switch (operation) {
			case 1:
				m.increaseFruit();
				break;
			case 2:
				m.recordSales();
				break;
			case 3:
				m.showStock();
				break;
			case 4:
				m.selectAllReocrds();
				break;
			case 5:
				m.increaseFruit();
				break;
			case 6:
				m.selectProfitByTime();
				break;
			case 7:
				m.showTopThree();
				break;
			case 8:
				m.showAll();
				break;
			default:
				break;
			}

		}

	}
	/**
	 * 进货
	 * @throws SQLException
	 */
	private void increaseFruit() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.print("输入进货的水果名：");
		String name = scanner.nextLine();
		System.out.print("输入进货价格：");
		String in = scanner.nextLine();
		BigDecimal inPrice = new BigDecimal(in);
		System.out.print("输入售价：");
		String out = scanner.nextLine();
		BigDecimal outPrice = new BigDecimal(out);
		System.out.print("输入进货的数量：");
		String a = scanner.nextLine();
		int amount = Integer.valueOf(a);
		Date date = new Date();
		Timestamp t = new Timestamp(date.getTime());
		FruitModel fruitModel = new FruitModel(name, inPrice, outPrice, amount);
		FruitInMedel inMedel = new FruitInMedel(name, amount, inPrice, t);
		inDao.insert(inMedel);// 插入进货记录
		int n = fruitDao.select(fruitModel);// 查询当前水果是否在水果表中存在记录
		if (n == 0) {// 如果不存在则插入
			fruitDao.insert(fruitModel);
		} else {// 存在则更新库存
			fruitDao.updateStock(inMedel);
		}

	}
	/**
	 * 记录销售
	 * @throws SQLException
	 */
	private void recordSales() throws SQLException {
		// TODO Auto-generated method stub
		BigDecimal b1 = new BigDecimal(0.5);
		BigDecimal b2 = new BigDecimal(0.8);
		BigDecimal discount = new BigDecimal(1.0);
		Scanner scanner = new Scanner(System.in);
		System.out.print("输入购买的水果种类：");
		String name = scanner.nextLine();
		FruitModel f1 = new FruitModel(name);
		//普通查询处理
//		int falg = fruitDao.select(f1);
//		if (falg == 0) {
//			System.out.println("不存在这种水果！");
//			return;
//		}
		//动态查询处理
		FruitModel temp1 = fruitDao.selectDynamic(f1);
		if(!name.equals(temp1.getFruitName())) {
			System.out.println("不存在这种水果！");
			return;
		}
		System.out.println("购买数量：");
		String s1 = scanner.nextLine();
		int weight = Integer.valueOf(s1);
		//普通查询处理
//		int stock = fruitDao.selectStock(f1);
		//动态查询处理
		int stock = fruitDao.selectDynamic(f1).getStock();
//		System.out.println(stock);
		if (stock < weight) {
			System.out.println("库存不足");
			return;
		}
		FruitModel f2 = new FruitModel(name, stock);
		// 扣减库存
		fruitDao.decreaseStock(weight, f2);
		//普通查询处理
//		BigDecimal inPrice = fruitDao.selectInPrice(f1);
//		BigDecimal outPrice = fruitDao.selectOutPrice(f1);
		//动态查询来处理
		BigDecimal inPrice = fruitDao.selectDynamic(f1).getInPrice();
		BigDecimal outPrice = fruitDao.selectDynamic(f1).getOutPrice();
		System.out.println("顾客性别:");
		String sex = scanner.nextLine();
		// 女顾客八折
		if (sex.equals("女")) {
			System.out.println("女性顾客享受八折优惠");
			outPrice = outPrice.multiply(b2);
			discount = discount.multiply(b2);
		}

		Date date = new Date();
		// 17点之后五折
		if (date.getHours() > 17) {
			System.out.println("17点之后半价优惠！");
			outPrice = outPrice.multiply(b1);
			discount = discount.multiply(b1);
		}
		Timestamp t = new Timestamp(date.getTime());
		SaleRecordModel saleRecordModel = new SaleRecordModel(name, weight, inPrice, outPrice, discount, sex, t);
		fruitRecodDao.insert(saleRecordModel);// 记录
		System.out.println("记录成功");
	}
	/**
	 * 显示库存
	 * @throws SQLException
	 */
	private void showStock() throws SQLException {
		// TODO Auto-generated method stub
		fruitDao.showAll();
	}
	/**
	 * 查询所有的销售记录
	 * @throws SQLException
	 */
	private void selectAllReocrds() throws SQLException {
		// TODO Auto-generated method stub
		fruitRecodDao.selectAll();
	}
	/**
	 * 查询指定时间段的利润
	 * @throws SQLException
	 */
	private void selectProfitByTime() throws SQLException {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		System.out.println("输入查询的起始时间");
		String start = scanner.nextLine();
		Timestamp startTime = Timestamp.valueOf(start);
		System.out.println("输入查询的终止时间");
		String end = scanner.nextLine();
		Timestamp endTime = Timestamp.valueOf(end);

		BigDecimal profit = fruitRecodDao.selectProfitByTime(startTime, endTime);
		System.out.println("在时间段" + startTime + " ~ " + endTime + "之内 的销售利润为：" + profit);

	}
	/**
	 *查询销量排名前三的水果
	 * @throws SQLException
	 */
	private void showTopThree() throws SQLException {
		// TODO Auto-generated method stub
		System.out.println("销量排名前三的：");
		fruitRecodDao.showTopThree();
	}
	/**
	 * 显示摊位详细信息
	 * @throws SQLException
	 */
	private void showAll() throws SQLException {
		// TODO Auto-generated method stub
		fruitRecodDao.selectInfo();
	}

	public void print() {
		System.out.println("----------------------");
		System.out.println("------摊位销售管理--------");
		System.out.println("1:添加进货");
		System.out.println("2:记录销售业务");
		System.out.println("3:显示当前各种水果的库存");
		System.out.println("4:查询所有的销售记录");
		System.out.println("5:添加其他品种的水果");
		System.out.println("6:查询指定日期间隔的销售利润");
		System.out.println("7:显示销售数量的前三名");
		System.out.println("8:显示摊位的详细信息");
		System.out.println("0:退出");

	}

}
